﻿using MelodysDemo.Application.Lists;

namespace MelodysDemo.Application.Answers
{
    public class AnswerService : IAnswerService
    {
        readonly IRepository<SurveyClazzQuestionnaire, int> _clazzQuestionnaire;//班级问卷信息表
        readonly IRepository<SurveyClazz, int> _clazz;//班级
        readonly IRepository<SurveyQuestionnaire, int> _questtionnaies;//问卷
        readonly IRepository<SurveyQuestion, int> _questtion;//问题
        readonly IRepository<SurveyOption, int> _option;//选项
        readonly IRepository<SurveyAnswer, int> _answer;//答题记录表
        readonly IRepository<SurveyAnswerDetail, int> _answerDetail;//答题详情记录表
        public AnswerService(IRepository<SurveyClazzQuestionnaire, int> clazzQuestionnaire,
                                             IRepository<SurveyClazz, int> clazz,
                                             IRepository<SurveyQuestionnaire, int> questtionnaies,
                                             IRepository<SurveyQuestion, int> questtion,
                                             IRepository<SurveyOption, int> option,
                                             IRepository<SurveyAnswer, int> answer,
                                             IRepository<SurveyAnswerDetail, int> answerDetail)
        {
            _clazzQuestionnaire = clazzQuestionnaire;
            _clazz = clazz;
            _questtionnaies = questtionnaies;
            _questtion = questtion;
            _option = option;
            _answer = answer;
            _answerDetail = answerDetail;
        }
       /* public Task<PagedResultDto<AnswerDto>> GetResultDto(AnswerInput input)
        {
            //通过 ClazzQuestionnaireId 获取 Answers 表的数据
            var aslist=_answer.GetAll()
                                           .Where(x=>x.ClazzQuestionnaireId==input.ClazzQuestionnaireId);
            //获取答题详情表的数据所属 Answers 表的数据
            List<SurveyAnswerDetail> sdList=new List<SurveyAnswerDetail>();
            foreach (SurveyAnswer ass in aslist)
            {
                var sd = _answerDetail.GetAll().Where(x => x.AnswerId == ass.Id);
                sdList.AddRange(sd);
            }
            //按问题的Id分组
            var optList = sdList.OrderByDescending(x => x.QuestionId)
                                                      .GroupBy(x => new { x.QuestionId, x.Question.Title })
                                                      .Select(x => new AnswerDto
                                                      {
                                                          Id = x.Key.QuestionId,
                                                          Title = x.Key.Title,
                                                          AvgScore = x.Average(x => x.Option.Score),
                                                          ScoreDList = sdList.Where(ad => ad.QuestionId == x.Key.QuestionId)
                                                                   .Select(x => new ScoreDto
                                                                   {
                                                                       count=x,
                                                                       Score=x.Option.Score
                                                                   }).ToList()//给每个问题的所有选项赋值
                                                      });
            return (Task<PagedResultDto<AnswerDto>>)optList;
        }*/
    }
}
